package com.metaera.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

public class SimpleMonthView extends MonthView {

    private int mPadding,mH,mW;
    private Paint mCurrentDayPaint = new Paint(), mSchemeBasicPaint;

    public SimpleMonthView(Context context) {
        super(context);

        mSchemeBasicPaint = new Paint();
        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(Color.RED);
        mSchemeBasicPaint.setFakeBoldText(true);

        mCurrentDayPaint.setAntiAlias(true);
        mCurrentDayPaint.setStyle(Paint.Style.FILL);
        mCurrentDayPaint.setColor(Color.parseColor("#883756FD"));

        mPadding = dipToPx(getContext(), 4);
        mH = dipToPx(getContext(), 4.0F);
        mW = dipToPx(getContext(), 4.0F);
    }

    @Override
    protected void onPreviewHook() {

    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(
                (float)(x + mPadding),
                (float)(y - mPadding),
                (float)(x + mItemWidth - mPadding),
                (float)(y + mItemHeight - mPadding),
                mSelectedPaint);
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        mSchemeBasicPaint.setColor(calendar.getSchemeColor());
        canvas.drawRect(
                (float)(mItemWidth / 2 + x - mW / 2),
                (float)(mItemHeight + y - mH * 2 - mPadding),
                (float)(x + mItemWidth / 2 + mW / 2),
                (float)(y + mItemHeight - mH - mPadding),
                mSchemeBasicPaint
        );
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;

        this.mCurDayTextPaint.setFakeBoldText(false);
        if (calendar.isCurrentDay() && !isSelected) {
            canvas.drawRect(
                    (float)(x + mPadding),
                    (float)(y - mPadding),
                    (float)(this.mItemWidth + x - this.mPadding),
                    (float)(this.mItemHeight + y - this.mPadding),
                    this.mCurrentDayPaint
            );
        }

        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
